package sketch.specs.annotation;

import java.lang.reflect.Method;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;

public class ClassUseRepTest extends TestCase {
	
	public static Test suite() {
		return new TestSuite(ClassUseRepTest.class);
	}
	
	public void testBasic() throws SecurityException, ClassNotFoundException {
		ClassUseRepTest.main(null);
	}

	@Repl(source= {"next", "hasNext"}, dest={"previous", "hasPrevious"})
	@TestSketch
	public void testIterator() {
		List<String> l = new LinkedList<String>();
		l.add("a");
		l.add("b");
		l.add("c");
		Iterator<String> it = l.iterator();
		
		while(it.hasNext()) {
			it.next();
		}
	}
	@Repls( repls ={@Repl(source = {"next", "hasNext"}, dest={"prev", "hasPrev"}),
	@Repl(source = {"next", "hasNext"}, dest={"previous", "hasPrevious"})})
	@TestSketch
	public void testIteratorMultiRepls() {
		List<String> l = new LinkedList<String>();
		l.add("a");
		l.add("b");
		l.add("c");
		Iterator<String> it = l.iterator();
		
		while(it.hasNext()) {
			it.next();
		}
	}
	
	public static int noice = 1;
	
	@Repl(source = {"weekday"}, dest={"monthday"})
	@TestSketch
	public void testReplaceString() {
		List<String> l = new LinkedList<String>();
		l.add("weekday");
		assertEquals("weekday", l.get(0));
	}
	
	public static void main(String[] args) throws SecurityException, ClassNotFoundException {
		for(Method m : Class.forName("sketch.specs.annotation.ClassUseRepTest").getDeclaredMethods()) {
			System.out.println("for method: " + m.getName());
			if(m.isAnnotationPresent(Repl.class)) {
				Repl rep = m.getAnnotation(Repl.class);
				String[] srcReplaces = rep.source();
				String[] destReplaces = rep.dest();
				for(String strR : srcReplaces) {
					System.out.println("source:  " + strR);
				}
				System.out.println("-----");
				for(String destR : destReplaces) {
					System.out.println("source:  " + destR);
				}
			}
			System.out.println(".+++");
		}
	}
	
	public void testAnnotationProcessor() throws ClassNotFoundException {
		Class<?> clz = Class.forName("sketch.specs.annotation.ClassUseRepTest");
		AnnotationProcessor processor = new AnnotationProcessor(clz);
		processor.process();
		for(Method m : clz.getDeclaredMethods()) {
			if(processor.getMappings(m) != null) {
				System.out.println(processor.getMappings(m));
			}
		}
	}	
}